<template>
  <div class="upload-container">
    <!-- 图片 -->
    <el-button :style="{background:color,borderColor:color}" class="upPic" icon="el-icon-upload" size="mini" type="primary" @click=" dialogVisible=true">上传图片
    </el-button>
    <el-dialog :visible.sync="dialogVisible">
      <el-upload
        :multiple="true"
        :file-list="fileList"
        :show-file-list="true"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        name="files"
        class="editor-slide-upload"
        :action="headUrl + 'oss/saveFileAndBind/'+this.$store.state.basicId + '/true'"
        list-type="picture-card">
        <el-button size="small" type="primary">点击上传</el-button>
      </el-upload>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </el-dialog>
    
    <!-- 视频 -->
      <el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" size="mini" type="primary" @click=" videoDialogVisible=true">上传视频
      </el-button>
      <el-dialog :visible.sync="videoDialogVisible">
        <el-upload
          :multiple="true"
          :file-list="videoList"
          :show-file-list="true"
          :on-remove="handleVideoRemove"
          :on-success="handleVideoSuccess"
          :before-upload="beforeVideoUpload"
          name="file"
          accept="video/mp4"
          class="editor-slide-upload"
          :action="headUrl + 'vodService/vod/uploadAndBind/'+this.$store.state.basicId"
          list-type="text">
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
        <el-button @click="videoDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleVideoSubmit">确 定</el-button>
      </el-dialog>
    
    
  </div>
</template>

<script>
import url from '../../../../config'
export default {
  props: {
    color: {
      type: String,
      default: '#1890ff'
    }
  },
  data() {
    return {
      dialogVisible: false,
      listObj: [],
      fileList: [],
      basicId: this.$store.state.basicId,  // 图片、视频上传需要的基本信息id，有时候报错
      imgLists: [],
      // 视频
      videoDialogVisible: false,
      videoList: [],
      videoId: '', // 上传视频获取id =====》 得到视频播放凭证
      vidath: {},
      vId: '',
      playAuth: '',
      headUrl: url.baseUrl, // ip地址与端口
    }
  },
  methods: {
    checkAllSuccess() {
      return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v])
      console.log(arr);
      // console.log(Object);
      if (!this.checkAllSuccess()) {
        this.$message('请等待所有图片上传成功 或 出现了网络问题，请刷新页面重新上传！')
        return
      }  
      this.$emit('successCBK', arr)   // 子传父传参
      this.listObj = {}
      this.fileList = []
      this.dialogVisible = false
    },
    handleSuccess(response, file, fileList) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      console.log(objKeyArr);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.data.images[0].imgUrl
        this.listObj[objKeyArr[i]].hasSuccess = true
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid
      const objKeyArr = Object.keys(this.listObj)
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]]
          return
        }
      }
    },
    beforeUpload(file) {
      const _self = this
      const _URL = window.URL || window.webkitURL
      const fileName = file.uid
      this.listObj[fileName] = {}
      return new Promise((resolve, reject) => {
        const img = new Image()
        img.src = _URL.createObjectURL(file)
        img.onload = function() {
          _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
        }
        resolve(true)
      })
      // console.log(file);
      // let arr = {}
      // arr.hasSuccess = false
      // arr.uid = file.uid
      // arr.width = file.width
      // arr.height = file.height
      // this.fileList.push(arr)
    },
    // 视频
    //上传视频
    handleVideoRemove(file, fileList) {
      console.log(123);
      console.log(file);
    },
    handleVideoSuccess(response, file, fileList) {
      console.log(file);
      this.videoId = response.data.video.id // 获取视频id
    },
    beforeVideoUpload(file) {

    },
    handleVideoSubmit() {
      this.$http.get("/vodService/vod/getPlayUrl/" + this.videoId)
      .then((res) => {
        if(res.data.status == 20000) {
          if(res.data.data.video.length) {
            let arr = res.data.data.video[0].vodUrl
            this.$emit('successVideo', arr)
          }else {
            this.$message.warning("上传视频发生未知错误，请重新上传")
          }
          this.videoDialogVisible = false
        }
      })
      .catch((err) => {
        console.log(err);
      })
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;
  /deep/ .el-upload--picture-card {
    width: 100%;
  }
}
.upPic {
  margin-right: 20px;
}
</style>
